package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsOrderGoodsMapper;
import com.qf.mapper.DtsOrderMapper;
import com.qf.mapper.DtsUserMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsOrder;
import com.qf.pojo.DtsOrderGoods;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsOrderService;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author 千峰教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Service
public class DtsOrderServiceImpl implements DtsOrderService {
    @Autowired
    private DtsUserMapper userMapper;
    @Autowired
    DtsOrderMapper orderMapper;

    @Autowired
    StatMapper statMapper;

    @Override
    public Integer findTotal() {

        LambdaQueryWrapper<DtsOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsOrder::getDeleted, false);

        Integer count = orderMapper.selectCount(wrapper);

        return count;
    }

    @Override
    public List<CategorySellAmts> findCategoryShellData() {
        return statMapper.findCategoryShellData();
    }

    @Override
    public List<DayStatis> queryDayStatis(int rang) {
        return statMapper.queryOrderStatis(rang);
    }



    @Override
    public IPage<DtsOrder> queryPage(Integer page, Integer limit, Integer orderStatusArray, String sort, String order, Integer userId, String orderSn) {

        IPage<DtsOrder> iPage = new Page<>(page, limit);

        QueryWrapper<DtsOrder> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(orderStatusArray)){
            wrapper.like("order_status",orderStatusArray);
        }

        if(order.equalsIgnoreCase("desc")){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        if(!StringUtils.isEmpty(userId)){
            wrapper.like("user_id",userId);
        }

        if(!StringUtils.isEmpty(orderSn)){
            wrapper.like("order_sn",orderSn);
        }

        iPage = orderMapper.selectPage(iPage, wrapper);

        return iPage;
    }

    @Override
    public Object addShip(Integer orderId, String shipChannel, String shipSn) {
        DtsOrder order = new DtsOrder();
        //101 未付款   用户奇效  系统取消
        // 201 已付款  202 申请退款 203申请退款
        //用户收货  系统收货
        if(StringUtils.isEmpty(shipChannel)){
            return ResponseUtil.fail(-1,"快递公司不能为空");

        }
        if(StringUtils.isEmpty(shipSn)){
            return ResponseUtil.fail(-1,"发货编号不能为空");

        }
        order.setShipSn(shipSn);
        order.setShipChannel(shipChannel);
        order.setShipTime(new Date());
        order.setUpdateTime(new Date());
        order.setId(orderId);
        order.setOrderStatus(402);
        int count = orderMapper.updateById(order);
        if(count > 0){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail(-1,"发货失败");
    }

    @Override
    public List<OrderIdAndGoods> findGoodsAndOrder(int day) {
        List<OrderIdAndGoods> orders = orderMapper.findGoodsOrder(day);
        return orders;

    }
    @Autowired
    private DtsOrderGoodsMapper ordersGoodsMapper;
    @Override
    public Object findById(Integer id) {
        DtsOrder order = orderMapper.selectById(id);

        DtsUser user = userMapper.selectById(order.getUserId());
        HashMap<String, Object> users = new HashMap<>();
        users.put("nickname",user.getNickname());
        users.put("avatar",user.getAvatar());
        List<DtsOrderGoods> orderGoods = ordersGoodsMapper.findByOrderId(id);
        HashMap<String, Object> data = new HashMap<>();
        data.put("orderGoods",orderGoods);
        data.put("user",users);
        data.put("order",order);
        return ResponseUtil.ok(data);

    }

    @Override
    public List<OrderCountVo> queryOrderCountVo(int rang) {
        return orderMapper.queryOrderStatis(rang);
    }


}
